package com.xcjh.utils;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class CommUtil {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    static String STATIC_FILE = "/static/";

    public static String getIp(String ip){
        return "http://"+ip + STATIC_FILE;
    }

    /**
     * 将源数据转换为 list<id>
     * @param listStr 源数据   1:时间1,2:时间2,3:时间3
     * @return 返回     List{1,2,3}
     */
    public static List<Long> findIds(String listStr){
        if (StringUtils.isEmpty(listStr)) {
            return null;
        }
        List<Long> collect = Arrays.stream(listStr.split(",")).map(str -> Long.parseLong(str.split(":")[0])).collect(Collectors.toList());
        return collect;
    }

    /**
     * 提取源数据的id
     * @param listStr 源数据   1:时间1,2:时间2,3:时间3
     * @return 返回     List{1,2,3}
     */
    public static boolean findCurrentIds(String listStr,Long id){
        List<Long> ids = findIds(listStr);
        return ids==null?false:ids.contains(id);
    }

    public static List<String> findMember(String listStr){
        if (StringUtils.isEmpty(listStr)) {
            return null;
        }
        List<String> collect = Arrays.stream(listStr.split(",")).collect(Collectors.toList());
        return collect;
    }
    public static String findCurrentMember(String listStr,Long id){
        return findMember(listStr).stream().filter(str -> Objects.equals(Long.parseLong(str.split(":")[0]), id))
                .collect(Collectors.toList()).stream().findFirst().orElse(null);
    }

    public static String getCurrentTime(){
        return sdf.format(new Date());
    }

}
